//
//  BaseCommondTabbarController.m
//  LoanMoney
//
//  Created by gynjobs on 2017/8/28.
//  Copyright © 2017年 cangming. All rights reserved.
//

#import "BaseCommondTabbarController.h"
#import "CustomTabbarItem.h"
#import "ServiceViewController.h"
#import "HomePageViewController.h"
@interface BaseCommondTabbarController ()<CustomTabbarItemDelegate>
@property(strong,nonatomic)UIView *customBarView;
@property(strong,nonatomic)CustomTabbarItem *TabbarItem;
@property(strong,nonatomic)NSMutableArray *TabrbarButtonArray;
@end

@implementation BaseCommondTabbarController


- (void)viewDidLoad {
    [super viewDidLoad];
    //隐藏本地View
    [self.tabBar setHidden:YES];
    self.customBarView.backgroundColor = [UIColor clearColor];
    if (!self.isNativeBar) {
        [self setViewControllerSForTabbar];
    }
}
-(UIView *)customBarView{
    __weak __typeof__(self) weakSelf = self;
    if (_customBarView==nil) {
        //添加底部的功能VIew
        UIView *customBarView = [[UIView alloc]init];
        customBarView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:customBarView];
        [customBarView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.view.mas_left);
            make.right.equalTo(weakSelf.view.mas_right);
            make.bottom.equalTo(weakSelf.view.mas_bottom);
            make.height.mas_equalTo(TABBARHIGHT);
        }];
        //全局
        _customBarView = customBarView;
    }
    return _customBarView;
}
-(void)setViewControllersArray:(NSMutableArray *)ViewControllersArray{
    if (_ViewControllersArray!=ViewControllersArray) {
        _ViewControllersArray = ViewControllersArray;
        [self addSubViewForCustomTabbar:ViewControllersArray];
    }
}
-(void)addSubViewForCustomTabbar:(NSMutableArray *)contentArray{
    __weak __typeof(&*self)weakSelf = self;
    
    CGFloat SubWidth = WINSIZEWIDTH/contentArray.count;
    for (int i=0; i<contentArray.count; i++) {
        CustomTabbarItem *ImageLable = [[CustomTabbarItem alloc]init];
        ImageLable.backgroundColor = [UIColor colorWithString:TABBARTINCOLOR];
        NSMutableDictionary *contentDic = [contentArray objectAtIndex:i];
        ImageLable.upImageName = [contentDic objectForKey:@"img"];
        ImageLable.imageSize = CGSizeMake(TabbarImageW, TabbarImageW);
        ImageLable.UpImageSelectName = [contentDic objectForKey:@"img_selected"];
        ImageLable.downLableString = [contentDic objectForKey:@"name"];
        ImageLable.canClick = YES;
        ImageLable.delegate = self;
        ImageLable.marginImageTop = TabbarMarginTop;
        ImageLable.tag = 1000+i;
        ImageLable.marginImageLable = TabbarMarginCenter;
        ImageLable.downLableColorNomalString = TabbarLableNomalColor;
        ImageLable.downLableColorSelectStrintg = TabbarLableSelectColor;
        [self.customBarView addSubview:ImageLable];
        [self.TabrbarButtonArray addObject:ImageLable];
        if (self.TabbarItem==nil) {
            [ImageLable mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(weakSelf.customBarView.mas_left);
                make.width.mas_equalTo(SubWidth);
                make.bottom.equalTo(weakSelf.customBarView.mas_bottom);
                make.top.equalTo(weakSelf.customBarView.mas_top);
            }];
        }else{
            [ImageLable mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.TabbarItem.mas_right);
                make.width.mas_equalTo(SubWidth);
                make.bottom.equalTo(weakSelf.customBarView.mas_bottom);
                make.top.equalTo(weakSelf.customBarView.mas_top);
            }];
        }
        self.TabbarItem = ImageLable;
    }
    //添加底部的功能VIew
    UIView *testView = [[UIView alloc]init];
    testView.backgroundColor = [UIColor colorWithString:TABBARLINECOLOR];
    [self.customBarView addSubview:testView];
    [testView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.customBarView.mas_top);
        make.left.equalTo(weakSelf.customBarView.mas_left);
        make.right.equalTo(weakSelf.customBarView.mas_right);
        make.height.mas_equalTo(1);
    }];
    
    /** 默认选择第一个 **/
    [self clickTheViewFor:[self.TabrbarButtonArray firstObject]];
    
}

-(void)setViewControllerSForTabbar{
    
    HomePageViewController *loanHome = [[HomePageViewController alloc]init];
    ServiceViewController *cheatsList = [[ServiceViewController alloc]init];
    //    WebViewController *loanTool = [[WebViewController alloc]init];
    //    loanTool.webURL =  HAODAI_TOOL_URL;
    //    loanTool.isCon = YES;
    //    loanTool.controllerString = @"工具";
    BaseCommondNaviController *loanHomeNa = [[BaseCommondNaviController alloc]initWithRootViewController:loanHome];
    BaseCommondNaviController *cheatsListNa = [[BaseCommondNaviController alloc]initWithRootViewController:cheatsList];
    NSArray *viewArray = @[loanHomeNa,cheatsListNa];
    self.viewControllers =viewArray;
}
#pragma mark ---点击的delegate---
-(void)clickTheViewFor:(CustomTabbarItem *)ClickView{
    for (int i=0; i<self.TabrbarButtonArray.count; i++) {
        CustomTabbarItem *iamgeLable = self.TabrbarButtonArray[i];
        iamgeLable.isSelect = NO;
    }
    ClickView.isSelect = YES;
    self.selectedIndex = ClickView.tag - 1000;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSMutableArray *)TabrbarButtonArray{
    if (_TabrbarButtonArray==nil) {
        _TabrbarButtonArray = [NSMutableArray array];
    }
    return _TabrbarButtonArray;
}
- (void)HiddenTheTabbar:(BOOL)isHidde{
    self.customBarView.hidden = isHidde;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
